
package network.client;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Timemachine {
    
    static private SimpleDateFormat formator;
    static private Timemachine self;
    
    private Timemachine () {
        formator = new SimpleDateFormat();
    }
    
    public static Timemachine getInstance(){
        if (self == null){self = new Timemachine();}
        return self;
    }
        
    public String formatDate(String format){
        Date now = new Date();
        formator.applyPattern(format);
        return formator.format(now);
    }        
    
    public String formatDate(String format, Date date){
        formator.applyPattern(format);
        return formator.format(date);
    }
    
    public Date getDate(String format, String datestring) throws ParseException{
        formator.applyPattern(format);
        Date date = formator.parse(datestring);
        return date;
    }
    
    public int getDaysOfMonth(int month, int year){
        Calendar cal = Calendar.getInstance();
        cal.set(year, month-1, 1);
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        return days;
    }
    
    public int getWeekdayAsInt(String format, String datestring) throws ParseException{
        return self.getWeekdayAsInt(self.getDate(format, datestring));
    }
    public int getWeekdayAsInt(Date date){
        int theOutput;
        String E = self.formatDate("E",date);
        System.out.println(E);
        if (E.equals("Mo")){
            theOutput = 0;
        } else if (E.equals("Di")){
            theOutput = 1;
        } else if (E.equals("Mi")){
            theOutput = 2;
        } else if (E.equals("Do")){
            theOutput = 3;
        } else if (E.equals("Fr")){
            theOutput = 4;
        } else if (E.equals("Sa")){
            theOutput = 5;
        } else if (E.equals("So")){
            theOutput = 6;
        } else {
            theOutput = 7;
        }
        return theOutput;
    }
    
    public String getWeekdayAsString(String format, String datestring) throws ParseException{
        return self.formatDate("E",self.getDate(format, datestring));
    }
    public String getWeekdayAsString(Date date) throws ParseException{
        return self.formatDate("E",date);
    }
    
    public static void main(String[] args) throws ParseException{
        Timemachine tm = Timemachine.getInstance();
        tm.getWeekdayAsInt(tm.getDate("dd.MM.yyyy","24.06.2012"));
    }
}
